/*
 * @Description: 
 * @Autor: HuiSir<273250950@qq.com>
 * @Date: 2024-05-29 18:45:47
 * @LastEditTime: 2024-06-20 14:25:07
 */
export function dateFmt(fmt, date) {
  var ret;
  var opt = {
    "Y+": date.getFullYear().toString(),        // 年
    "m+": (date.getMonth() + 1).toString(),     // 月
    "d+": date.getDate().toString(),            // 日
    "H+": date.getHours().toString(),           // 时
    "M+": date.getMinutes().toString(),         // 分
    "S+": date.getSeconds().toString()          // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (var k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    }
  }
  return fmt;
}

export function listToTree({ list, parentId = null, idKey = 'id', parentIdKey = 'parentId', childrenKey = 'children' }) {
  const tree = [];

  for (let item of list) {
    if (item[parentIdKey] === parentId) {
      const children = listToTree({
        list,
        parentId: item[idKey],
        idKey,
        parentIdKey,
        childrenKey
      });
      if (children.length) {
        item[childrenKey] = children;
      }
      tree.push(item);
    }
  }

  return tree;
}